﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using System.Collections;
using System.Data.Objects;
using System.Data.Objects.SqlClient;


namespace XBoshiGFS.reports
{
    public partial class WaterUsageInputForm : Form
    {
        XBoshiGFSModelContainer context = new XBoshiGFSModelContainer();
        IQueryable  orgSql;
        String strGroupID;
        IList<WaterUsageInputData> cachedDataList=new  List<WaterUsageInputData>(); 




        public WaterUsageInputForm()
        {
            InitializeComponent();

            cmbStation.DataSource = (from p in context.StationSet select p);

            //灌季
            cmbWaterSeason.DataSource = (from p in context.WaterSeasonSet where p.strState == "灌季中" select p);

            feeStandardSetBindingSource.DataSource = (from p in context.FeeStandardSet select p);


            this.dateTimePicker1.MinDate = DateTime.Now.AddYears(-2) ;
            this.dateTimePicker1.MaxDate = DateTime.Now.AddYears(1) ;
            this.dateTimePicker1.Value = DateTime.Now;

        }
 
        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {
                strGroupID = "-1";

                try
                {
                 }
                catch (Exception ex) {

                    var a = ex.InnerException;
                } 
        }


        private void cmbStation_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbSegment.DataSource = (from p in context.SegmentCourseSet where p.StationId == strID orderby p.strName select p);
        }

        private void cmbSegment_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbWaterCourse.DataSource = (from p in context.WaterCourseSet where p.SegmentCourseId == strID orderby p.strName select p);

        }

        private void cmbWaterCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbVillage.DataSource = (from p in context.VillageSet where p.WaterCourseId == strID orderby p.strName select p);

        }

        private void cmbVillage_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbGroup.DataSource = (from p in context.GroupSet where p.VillageId == strID orderby p.strName select p);

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                strGroupID = (String)cmbGroup.SelectedValue;
                var sql = GetQuerySql(strGroupID);
                waterUsageInputDataBindingSource.DataSource = sql;

            }catch(Exception ex){
            
            } 
        }

        private IList<WaterUsageInputData>   GetQuerySql(String strGroupID)
        {

            String strWaterSeansonID = (String)cmbWaterSeason.SelectedValue;

            var orgSql =
            from u in context.WaterUsageSet 
            join t in context.PeopleSet on  u.PeopleId  equals t.Id 
            join fee in context.FeeStandardSet on u.FeeStandardId  equals fee.Id 
            where t.GroupId == this.strGroupID   && u.WaterSeasonId == strWaterSeansonID
            orderby t.strCode
            select new // WaterUsageInputData  
            {
                Id=u.Id ,
                BeginTime =u.BeginTime ,
                EndTime =u.EndTime ,
                FlowRate =u.FlowRate ,
                strPeopleCode =t.strCode ,
                strPeopleName =t.strName ,
                UseArea =  u.UseArea==null?0.0M:u.UseArea.Value ,
                UseLong = u.UseLong == null ? 0.0M : u.UseLong,
                WaterBill = u.WaterBill == null ? 0.0M : u.WaterBill.Value,
                WaterCap =u.WaterCap,
                decPriceAll= fee.PriceAll,
                strFeeType=fee.Id 
            };

            List<WaterUsageInputData> inputList = new List<WaterUsageInputData>();
            foreach(var u  in orgSql){
                WaterUsageInputData input = new WaterUsageInputData()
                {
                    Id = u.Id,
                    BeginTime = u.BeginTime,
                    EndTime = u.EndTime,
                    FlowRate = u.FlowRate,
                    strPeopleCode = u.strPeopleCode,
                    strPeopleName = u.strPeopleName,
                    UseArea = u.UseArea,
                    UseLong = u.UseLong,
                    WaterBill = u.WaterBill,
                    WaterCap = u.WaterCap,
                    decPriceAll=u.decPriceAll.Value   ,
                    strFeeType=u.strFeeType 

                };
                inputList.Add(input); 
            }



            return inputList;
        } 


        private void cmbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
    
        }

        private void cmbWaterSeason_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnQuery.Enabled = true;
            this.btnInit.Enabled = true; 
        }

        private void WaterBillReportForm_Load(object sender, EventArgs e)
        {

        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

 
        private void waterUsageSetDataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            WaterUsageInputData wuid = (WaterUsageInputData)waterUsageInputDataBindingSource.Current;

            if (dateTimePicker1.Value.Year == 2001)
            {
                MessageBox.Show("请选择灌溉日期", "提示");
                e.Cancel = true;
                return;
            }
            wuid.BeginTime = dateTimePicker1.Value; 
            
            if (Int32.Parse(TBFlowRate.Text) == 0)
            {
                MessageBox.Show("请输入灌溉流量", "提示");
                e.Cancel = true;
                return;
            }
            wuid.FlowRate = decimal.Parse(TBFlowRate.Text);  
        }

        private void waterUsageSetDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            WaterUsageInputData wuid = (WaterUsageInputData)waterUsageInputDataBindingSource.Current;
            wuid.WaterCap = wuid.UseLong *wuid. FlowRate * 60 / 1000;
            wuid.WaterBill = wuid.WaterCap * wuid.decPriceAll ;
            cachedDataList.Add(wuid); 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            foreach (var wuid   in cachedDataList) {
                WaterUsageSet wus=(from p in context.WaterUsageSet where p.Id==wuid.Id select p).FirstOrDefault ();
                wus.FlowRate =wuid.FlowRate ;
                wus.BeginTime =wuid.BeginTime ;
                wus.UseArea =wuid.UseArea ;
                wus.UseLong =wuid.UseLong ;
                wus.WaterBill =wuid.WaterBill ;
                wus.WaterCap =wuid.WaterCap ;
                wus.FeeStandardId = wuid.strFeeType; //水费类型
                context.SaveChanges ();  
            }

            if (true)
            {   //保存成功，清除列表，刷新数据。
                cachedDataList.Clear();
            }

        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            //进行初始化

            String strWaterSeansonID = (String)cmbWaterSeason.SelectedValue;
            String strGroupID = (String)cmbGroup.SelectedValue;
            if (String.IsNullOrWhiteSpace(strGroupID)) {
                MessageBox.Show("请选择组！");    
                return ;
            
            }
            if (String.IsNullOrWhiteSpace(strWaterSeansonID))
            {
                MessageBox.Show("请选择灌季！");
                return;

            }


            var season = (from p in context.WaterSeasonSet where p.Id == strWaterSeansonID select p).FirstOrDefault();


            var peopleset = (from p in context.PeopleSet where p.GroupId ==strGroupID  select p);
            foreach (var people in peopleset)
            {


                WaterUsageSet wus ;
                wus=(from p in context.WaterUsageSet where p.PeopleId ==people.Id && p.WaterSeasonId ==strWaterSeansonID select p  ).FirstOrDefault ();
                if (wus == null)
                {
                    wus = new WaterUsageSet();
                    wus.Id = Guid.NewGuid().ToString();
                    context.AddToWaterUsageSet(wus);
                    wus.PeopleId = people.Id;
                    wus.UseArea = people.UseArea == null?0:people.UseArea ;
                    wus.WaterSeasonId = season.Id;
                    wus.FeeStandardId = season.FeeStandardSetId;

                    wus.UseLong = 0.0M ;
                    wus.WaterBill = 0.0M;
                    wus. WaterCap =0.0M;
                    wus.FlowRate = 0;
                }

            }
            //保存数据
             context.SaveChanges();
             MessageBox.Show ("数据初始化完成！");    
        }





    }
}
